/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-01-31
 * Time: 12:42
 */
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution4 {
    public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null){//如果当前节点为null或者只有一个节点，就返回
            return head;
        }
        //后面整体进行交换，然后用一个指针记录一下头结点
        ListNode tmp = swapPairs(head.next.next);
        // 最后需要返回的节点也要记录一下
        ListNode result = head.next;
        result.next = head;
        head.next = tmp;
        return result;
    }
}
public class Test4 {
}
